//
//  ZBDebugView.m
//  DevDemo
//
//  Created by BO ZHOU on 2021/5/6.
//

#import "ZBDebugView.h"
#import "ZBDebugCollectionViewCell.h"
#import "ZBDebugSwitchUserViewController.h"

@interface ZBDebugView()<UICollectionViewDelegate,UICollectionViewDataSource>



/// collectionview
@property (nonatomic, strong) UICollectionView* collectionview;

/// ui模型
@property (nonatomic, strong) NSArray<ZBDebugViewSourceModel*>* models;

@end

@implementation ZBDebugView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.collectionview];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.collectionview.frame = self.bounds;
}

- (void)expandDidComplete {
    [UIView animateWithDuration:0.25 animations:^{
        self.collectionview.alpha = 1;
    }];
}


- (void)setIsExpande:(bool)isExpande {
    
    if (self.isExpande == isExpande) {
        return;
    }
    
    super.isExpande = isExpande;
    
    if (!isExpande) {
        self.collectionview.alpha = 0;
    }
}


- (UICollectionView *)collectionview {
    if (_collectionview == nil) {
        
        CGFloat wh = self.expandWH/3;
        
        UICollectionViewFlowLayout* layout = [UICollectionViewFlowLayout new];
        layout.itemSize = CGSizeMake(wh, wh);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        
        _collectionview = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionview.delegate = self;
        _collectionview.dataSource = self;
        [_collectionview registerClass:[ZBDebugCollectionViewCell class] forCellWithReuseIdentifier:@"ZBDebugCollectionViewCell"];
        _collectionview.alpha = 0;
        _collectionview.pagingEnabled = true;
        _collectionview.backgroundColor = [UIColor clearColor];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.collectionview.layer.cornerRadius = self.layer.cornerRadius;
            self.collectionview.layer.masksToBounds = true;
        });
    }
    return _collectionview;
}

- (NSArray<ZBDebugViewSourceModel *> *)models {
    NSMutableArray* mmodels = [NSMutableArray array];
    
    [mmodels addObject:[ZBDebugViewSourceModel modelWithIconName:@"logout" title:@"退出登录" functionType:ZBDebugFunctionType_logout]];
    [mmodels addObject:[ZBDebugViewSourceModel modelWithIconName:@"qiehuanzhanghao" title:@"切换账户" functionType:ZBDebugFunctionType_switchUsers]];
    [mmodels addObject:[ZBDebugViewSourceModel modelWithIconName:@"API" title:@"API日志" functionType:ZBDebugFunctionType_apiLog]];
    [mmodels addObject:[ZBDebugViewSourceModel modelWithIconName:@"qiehuan" title:@"切换baseUrl" functionType:ZBDebugFunctionType_switchBaseUrl]];
    [mmodels addObject:[ZBDebugViewSourceModel modelWithIconName:@"jiance" title:@"fps卡顿监测" functionType:ZBDebugFunctionType_monitorFps]];

    return mmodels;
}


//+ (void)setLogoutBlock:(void (^)(void))logoutBlock {
//    ZBDebugView* view = [self shared];
//    
//    view.logoutBlock = logoutBlock;
//}
//
//
//+ (void)setSwitchUsersBlock:(void (^)(NSString * _Nonnull, NSString * _Nonnull))switchUsersBlock {
//    ZBDebugView* view = [self shared];
//    
//    view.switchUsersBlock = switchUsersBlock;
//}




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.models.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZBDebugCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZBDebugCollectionViewCell" forIndexPath:indexPath];
    
    cell.model = self.models[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZBDebugViewSourceModel* model = self.models[indexPath.row];
    
    if (model.functionType == ZBDebugFunctionType_logout) {
        if (self.logoutBlock) {
            self.logoutBlock();
        }
    } else if (model.functionType == ZBDebugFunctionType_switchUsers) {
        ZBDebugSwitchUserViewController* controller = ZBDebugSwitchUserViewController.new;
        controller.didSelectAccountBlock = ^(ZBDebugUserInfoModel * _Nonnull model) {
            if (self.switchUsersBlock) {
                self.switchUsersBlock(model.userName, model.password);
            }
        };
        
        UINavigationController* navcon = [[UINavigationController alloc]initWithRootViewController:controller];
        navcon.modalPresentationStyle = UIModalPresentationCustom;
        [[self topViewController] presentViewController:navcon animated:true completion:nil];
    }
}


-(UIViewController*)topViewController {
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    
    UIViewController* rootViewController = window.rootViewController;
    
    while (rootViewController.presentedViewController) {
        rootViewController = rootViewController.presentedViewController;
    }
    
    return [self topViewControllerForRoot:rootViewController];
}

-(UIViewController*)topViewControllerForRoot:(UIViewController*)rootViewController {
    if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        return [self topViewControllerForRoot:((UINavigationController*)rootViewController).childViewControllers.lastObject];
    } else if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        return [self topViewControllerForRoot:((UITabBarController*)rootViewController).selectedViewController];
    } else {
        return rootViewController;
    }
}


@end
